package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.AddRoleRequest;
import com.sc.nft.admin.request.QueryRoleRequest;
import com.sc.nft.admin.request.UpdateRoleRequest;
import com.sc.nft.admin.vo.RoleVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.Role;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.service.RoleService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 角色表服务控制器
 *
 * @author zhangxiangyu
 * @since 2021-04-03 15:15:03
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/role")
@Api(tags="后台角色模块")

public class RoleController   {
    private final RoleService roleService;
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建角色",notes = "创建角色",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public Result add(@RequestBody AddRoleRequest addRole) throws GlobalException {
        if(roleService.checkRoleNameExists(addRole.getRoleName())){
            return Result.fail(ErrorCode.ROLE_EXISTS);
        }
        Role role = new Role();
        BeanUtils.copyProperties(addRole,role);
        return Result.success(role.insert());
    }


    @PostMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新角色", notes = "更新角色",httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public Result update(@RequestBody UpdateRoleRequest updateRole) throws GlobalException {
        if(roleService.checkRoleNameExists(updateRole.getRoleName())){
            return Result.fail(ErrorCode.ROLE_EXISTS);
        }
        Role role = new Role();
        BeanUtils.copyProperties(updateRole,role);
        return Result.success(role.updateById());
    }


    @PostMapping(value="/query", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询角色", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public PageResult<RoleVO>  query(@RequestBody QueryRoleRequest queryRole) throws GlobalException {
        return PageResult.success(  roleService.queryRoles(queryRole.getRoleName(),queryRole.getPageNo(),queryRole.getPageSize()), RoleVO.class);
    }




}